home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / Sources / FWPriMem.cpp next >
Encoding:
Text File  |  1996-08-16  |  4.6 KB  |  160 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWPRIMEM_H
  13. #include "FWPriMem.h"
  14. #endif
  15.  
  16. #ifndef SLPRIDEB_H
  17. #include "SLPriDeb.h"
  18. #endif
  19.  
  20. #ifdef FW_BUILD_MAC
  21. #pragma segment FWCommon
  22. #endif
  23.  
  24. typedef void (*pfvv)();
  25. extern pfvv set_new_handler(pfvv);
  26.  
  27. //========================================================================================
  28. // set_new_handler
  29. //========================================================================================
  30.  
  31. // We provide our own definition of set_new_handler mainly as a precaution against
  32. // ending up with a shared library version of set_new_handler.  ODF as a whole has been
  33. // written assuming that operator new will throw an exception if it fails to allocate
  34. // memory.  In a runtime environment such as OpenDoc, it's possible for multiple shared
  35. // libraries to exist in once process.  An exception thrown from code in one shared
  36. // library should not propogate outside of the shared library (since there is no agreed
  37. // binary interface for exceptions across shared libraries).  This implies that if
  38. // operator new throws exceptions that operator new cannot be implemented in a shared
  39. // library.  In actuality, it's the "new hander" that throws, so there must be one new
  40. // handler per shared library, and thus one version of set_new_handler per shared library.
  41. // This unit (FWPriMem.cpp) is intended to be statically linked into each shared library
  42. // built with ODF.
  43.  
  44. #if defined(FW_DONT_USE_STD_LIB_SET_NEW_HANDLER)
  45.  
  46.     static pfvv gNewHandler = 0;
  47.     
  48.     #ifndef _MSC_VER
  49.     
  50.     pfvv set_new_handler(pfvv handler)
  51.     {
  52.         // See ARM, pp 280-81.
  53.         pfvv oldHandler = gNewHandler;
  54.         gNewHandler = handler;
  55.         return oldHandler;
  56.     }
  57.     
  58.     #endif
  59.     
  60.     #ifdef _MSC_VER
  61.     
  62.     #include <new.h>
  63.     
  64.     pfvv __cdecl set_new_handler(pfvv handler)
  65.     {
  66.         // See ARM, pp 280-81.
  67.         pfvv oldHandler = gNewHandler;
  68.         gNewHandler = handler;
  69.         return oldHandler;
  70.     }
  71.     
  72.     // [KVV] Because of the way Visual C++ 4.0 linker/library work,
  73.     //    we have to define the following stuff
  74.     
  75.     _PNH _set_new_handler(_PNH /* pnh */)
  76.     {
  77.         return 0;
  78.     }
  79.     
  80.     _PNH _query_new_handler()
  81.     {
  82.         return 0;
  83.     }
  84.     
  85.     extern "C" int _callnewh(size_t /* size */)
  86.     {
  87.         return 1;
  88.     }
  89.     
  90.     #endif
  91.     
  92. #endif
  93.  
  94. //========================================================================================
  95. // C++ Global operator new & delete
  96. //========================================================================================
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // operator new, placed memory version
  100. //----------------------------------------------------------------------------------------
  101.  
  102. // already defines placement new in <new.h>
  103. //    Turn off definition if we ought not define it
  104. #define FW_NEW_WITH_PLACEMENT
  105.  
  106. #if defined(_MSC_VER)
  107. #undef    FW_NEW_WITH_PLACEMENT
  108. #elif defined(SYMANTEC_CPLUS)
  109. #undef    FW_NEW_WITH_PLACEMENT
  110. #elif defined(__MWERKS__)
  111. #if __option(dont_inline)
  112. #undef    FW_NEW_WITH_PLACEMENT
  113. #endif
  114. #endif
  115.  
  116. #ifdef FW_NEW_WITH_PLACEMENT
  117. void *operator new(size_t /* size */,void *p)
  118. {
  119.     return p;
  120. }
  121. #endif
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // operator new
  125. //----------------------------------------------------------------------------------------
  126. void *operator new(size_t size)
  127. {
  128.     void* p;
  129.     
  130.     while ((p = FW_PrimitiveAllocateBlock(size)) == NULL)
  131.     {
  132.         pfvv newHandler;
  133.         // this is just a cheap way to get the newHandler function pointer
  134.         set_new_handler(newHandler = set_new_handler(0));
  135.         FW_PRIV_ASSERT(newHandler != NULL);
  136.         // ODF is written assuming that operator new will not return if it fails to
  137.         // allocate memory.  It is assumed that a newHandler will be installed which
  138.         // throws an exception.  ODF installs a newHandler in a higher layer (FWODExce)
  139.         // so a newHandler will exist unless a developer chooses to intentionally
  140.         // deinstall the newHandler by calling ::set_new_handler(0).  Doing so will
  141.         // break ODF's error handling strategy and is considered illegal.  Note
  142.         // that developers are free to install their own newHandler.
  143.         if (!newHandler)
  144.             return 0;
  145.         else
  146.             newHandler();
  147.     }
  148.  
  149.     return p;
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------
  153. // operator delete
  154. //----------------------------------------------------------------------------------------
  155. void operator delete(void *p)
  156. {
  157.     FW_PrimitiveFreeBlock(p);
  158. }
  159.  
  160.